Skip to content

Conversation

@denverbaumgartner
Copy link
Contributor

Community Testcontainers Implementation for Foundry Anvil (state persistence)

Extends the work done in #272 to support saving and loading state from the Anvil image, as supported by the official Anvil implementation (Anvil reference).

Usage example

use testcontainers_modules::{
	anvil::AnvilNode,
	testcontainers::runners::AsyncRunner,
};
use alloy_network::AnyNetwork;
use alloy_provider::{Provider, RootProvider};

// Mount a host dir, load a previous snapshot, keep dumping updates, and checkpoint periodically.
let node = AnvilNode::latest()
	.with_chain_id(1)
	.with_fork_url("https://eth.merkle.io")
	.with_state_mount("/absolute/host/dir")          // e.g. /tmp/anvil-state
	.with_load_state_path("/state/state.json")       // inside the container
	.with_dump_state_path("/state/state.json")       // inside the container
	.with_state_interval(5)                          // seconds
	.start()
	.await
	.unwrap();

let port = node.get_host_port_ipv4(8545).await.unwrap();
let provider: RootProvider<AnyNetwork> =
	RootProvider::new_http(format!("http://localhost:{port}").parse().unwrap());

let block_number = provider.get_block_number().await.unwrap();

@denverbaumgartner
Copy link
Contributor Author

Don't believe the failing tests are due to changes in this PR. Do let me know if there are any requested changes.

@DDtKey
Copy link
Contributor

DDtKey commented Sep 18, 2025

Thank you for the contribution!

Yes, these are flaky tests we aware of. It should be fixed separately and not related to the changes

@DDtKey DDtKey merged commit 60331dc into testcontainers:main Sep 18, 2025
17 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants